#include <windows.h>
#include <iostream>
#include <sstream>
#include <ctime>
#include <vector>
#include <fstream>
#include "gl\gl.h"
#include "gl\glu.h"
#include "gl\glut.h"
#include "gl\glaux.h"
#include "tgaLoader.h"
#include "player.h"
#include "imageLoader.h"
#include "models/md2model.h"
#include "constants.h"
#include "math.h"
#include "error.h"
#include "handyFunctions.h"
#include "gameParameters.h"
#include "hud.h"

using namespace std;

void Hud::renderSpacedBitmapString(
			float x, 
			float y,
			int spacing, 
			void *font,
			char *string) {
  char *c;
  int x1=x;
  for (c=string; *c != '\0'; c++) {
	glRasterPos2f(x1,y);
    	glutBitmapCharacter(font, *c);
	x1 = x1 + glutBitmapWidth(font,*c) + spacing;
  }
}

//function that allows for
//drawing in a 2D world
void Hud::glBegin2D()
{
	glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();

    glOrtho(0, 800, 0, 600, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
}

//finishes drawing in a 2D world
void Hud::glEnd2D()
{
	glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
}

void Hud::draw(int hp, int ammo, int frags, int deaths, float alpha, int pdTime = -1, int fmTime = -1)
{
		char str[20];
		glBegin2D();

		glTranslated(400, 300, 0);
		glColor4d(255, 255, 255, 1);

		glDisable(GL_DEPTH_TEST);
		glDisable(GL_LIGHTING);
		glDisable(GL_TEXTURE_2D);

		glBegin(GL_LINES);
			glVertex2d(0, 0);
			glVertex2d(0, 10);
			glVertex2d(-5, 5);
			glVertex2d(5, 5);
		glEnd();

		glTranslated(-400, -300, 0);

		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA,GL_ONE);
		glColor4f(0.5f, 0.0f, 0.0f, alpha);
		glBegin(GL_QUADS);
			glVertex2d(0,0);
			glVertex2d(0,600);
			glVertex2d(800,600);
			glVertex2d(800,0);
		glEnd();
		glDisable(GL_BLEND);

		glColor4d(255, 255, 255, 1);

		renderSpacedBitmapString(30,60,1,GLUT_BITMAP_HELVETICA_18,"HP: ");
		sprintf_s(str, hp, "%s");
		renderSpacedBitmapString(130,60,1,GLUT_BITMAP_HELVETICA_18,str);

		renderSpacedBitmapString(30,40,1,GLUT_BITMAP_HELVETICA_18,"AMMO: ");
		sprintf_s(str, ammo, "%s");
		renderSpacedBitmapString(130,40,1,GLUT_BITMAP_HELVETICA_18,str);

		renderSpacedBitmapString(30,20,1,GLUT_BITMAP_HELVETICA_18,"RESULT: ");
		sprintf_s(str, frags, "%d", deaths, "/%d");
		renderSpacedBitmapString(130,20,1,GLUT_BITMAP_HELVETICA_18,str);

		if(pdTime >= 0)
		{
			renderSpacedBitmapString(240,20,1,GLUT_BITMAP_HELVETICA_18,"PENTA-DAMAGE: ");
			sprintf_s(str, pdTime, "%s");
			renderSpacedBitmapString(420,20,1,GLUT_BITMAP_HELVETICA_18,str);
		}

		if(fmTime >= 0)
		{
			renderSpacedBitmapString(240,40,1,GLUT_BITMAP_HELVETICA_18,"FLY-MODE: ");
			sprintf_s(str, fmTime, "%s");
			renderSpacedBitmapString(420,40,1,GLUT_BITMAP_HELVETICA_18,str);
		}

		glEnable(GL_TEXTURE_2D);
		glEnable(GL_DEPTH_TEST);
		glEnable(GL_LIGHTING);
		glEnd2D();
}